// pages/device/index.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusList: [
      { label: '故障频次', value: '100' },
      { label: '平均故障率', value: '15%' },
      { label: '维修效率', value: '良好' },
    ],
    keyword: '',
    showDrawer: false,
    showCalendar: false,
    dateRange: {},
    filterCount: 3,
    warnCount: 0,
    deviceList: [
    ], // 整个数组类型断言
    filterCate: [
      { label: '全部', value: '0' },
      { label: '故障', value: '1' },
      { label: '可疑', value: '2' },
      { label: '良好', value: '3' },
      { label: '离线', value: '4' },
    ],
    filterList: [
      {
        cateName: '幼儿园项目',
        cateList: [{
          name: '西昌之心幼儿园',
          isActive: true,
        }, {
          name: '外国语幼儿园',
          isActive: false,
        }, {
          name: '川美幼儿园',
          isActive: true,
        }, {
          name: '蓝天幼稚园',
          isActive: false,
        }, {
          name: '春田幼儿园',
          isActive: false,
        }, {
          name: '东路幼儿园',
          isActive: true,
        },]
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

    this.getDeviceList()
    // this.getWarnInfo()
  },
  getDeviceList() {
    let _this = this
    app.axios('item', {
      op: "ajaxCusGroupDcAll",
    }).then(res => {
      if (res.code) {
        const data = []
        const onlineData = res.tdstaarr
        const camlist = res.camlis
        res.lists && res.lists.forEach(item => {
          if (item.zztype === 2) {
            const children = []
            camlist.forEach(cam => {
              if (item.zid === cam.rid) {
                children.push(cam)
              }
            })
            const dataItem = {
              id: item.zid,
              name: item.label,
              img: app.getImgUrl('camera1.svg'),
              online: String(onlineData[item.zid] && onlineData[item.zid].online ? onlineData[item.zid].online : 0),
              total: String(onlineData[item.zid] && onlineData[item.zid].all ? onlineData[item.zid].all : 0),
              children: children,
              location:'山东省济南市历下区',//缺字段
              model:'MN-ZOE0021',//缺字段
              img:app.getImgUrl('camera-ico.png')//缺字段
            }
            data.push(dataItem)
          }
        })
        console.log(Object.getOwnPropertyNames(res.warrlist).length)
        _this.setData({
          deviceList: data,
          warnCount: Object.getOwnPropertyNames(res.warrlist).length
        })
      }
    })
  },
  getWarnInfo() {
    let _this = this
    app.axios('user', {
      op: "ajaxWelData",
      zstatus: 1,
    }).then(res => {
    })
  },
  onShareAppMessage() {
    // 保持空实现
    return {
      title: '设备列表',
      path: '/pages/device/index'
    };
  },

  /**
   * 输入框变化事件
   */
  onChange(event) {
    console.log('输入值:', event.detail.value);
  },

  /**
 * 抽屉相关函数
 */
  toggleDrawer() {
    // 直接调用组件方法（需获取实例）
    const drawer = this.selectComponent('#v-drawer')
    drawer.toggleDrawer()
  },

  onDrawerChange(e) {
    console.log('抽屉状态:', e.detail.visible)
  },

  onDrawerClose() {
    this.setData({ showDrawer: false })
  },
  showCalendar() {
    this.setData({ showCalendar: true });
  },
  onCloseCalendar() {
    this.setData({ showCalendar: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirmCalendar(event) {
    const [start, end] = event.detail;
    this.setData({
      showCalendar: false,
      // date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
      dateRange: { startDate: this.formatDate(start), endDate: this.formatDate(end) }
    });
  },
  navigateTo(e) {
    const url = e.currentTarget.dataset.url
    app.navigateTo(url)
  }
});